/**
 * 手撕——二叉排序树
 */
public class TwoSortTree {

    /**
     * 二叉树的节点
     */
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }
    }


    /**
     * 定义一个根节点
     */
    public TreeNode root;


    /**
     * 查找元素val是否存在
     * @param val 查找的值
     * @return
     */
    public boolean search(int val) {
        if(root == null) return false;
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val < val) {
                cur = cur.right;
            }else if(cur.val > val){
                cur = cur.left;
            }else {
                return true;
            }
        }
        return false;
    }

    /**
     *  插入一个值
     * @param val
     * @return
     */
    public boolean insert(int val) {
        TreeNode node = new TreeNode(val);
        if(root == null) {
            root = node;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = root;
        while (cur != null) {
            if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val) {
                parent = cur;
                cur = cur.left;
            }else {
                return false;//禁止插入相同的值
            }
        }
        if(parent.val < val) parent.right = node;
        else parent.left = node;
        return true;
    }

    /**
     *  删除结点
     *
     * @param val
     */
    public void delete(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        //1.先找到待删除结点和其父节点
        while (cur != null) {
            if(cur.val < val) {
                //在右边
                parent = cur;
                cur = cur.right;
            }else if(cur.val > val) {
                //在左边
                parent = cur;
                cur = cur.left;
            }else {
                deleteNode(parent,cur);
            }
        }

    }

    //负责删除结点
    private void deleteNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            //删除结点的左部分为null
            //1.cur为头结点
            //2.cur为parent的left
            //3.cur为parent的right
            if(cur == root) {
                root = cur.right;
            }else if(parent.left == cur) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }

        }else if(cur.right == null) {
            //删除结点的右部分为null
            //1.cur为头结点
            //2.cur为parent的left
            //3.cur为parent的right
            if(cur == root) {
                root = cur.left;
            }else if(parent.left==cur) {
                parent.left=cur.left;
            }else {
                parent.right=cur.left;
            }
        }else {
            //两边都不为null -- 使用覆盖删除法
            //1.使用删除结点的左树最大值覆盖cur，并且删除 -- 保证cur左边都比cur小 并且可以保证没有右子树
            //2.使用删除结点的右树最小值覆盖cur，并且删除。并且可以保证没有左子树
            //(1)使用右子树的最小值替换
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;//真正删除结点
            if(targetParent.right == target) {
                //target在父节点右边
                targetParent.right = target.right;
            }else {
                //target在父节点左边
                targetParent.left = target.right;
            }

            //(2) 使用左子树替换
            /**
             *
            TreeNode target = cur.left;
            TreeNode targetParent = cur;
            while (target.right != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;//真正删除结点
            if(targetParent.right == target) {
                //target在父节点右边
                targetParent.right = target.left;
            }else {
                //target在父节点左边
                targetParent.left = target.left;
            }

            */


        }

    }


}
